define(function(require, exports) {
	var util = require('util');
	var pubjs = require('pubjs');
	var view = require('@base/view');

	// 查看广告位分组
	var Base = view.widget.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'layout': {
					'module': '@base/dialog.base',
					'width': 900,
					'title': LANG("广告位列表"),
					'buttons': ['ok'],
					'silence': false
				},
				'gridConfig': {
					'url': 'spot/rtbList',
					'auto_load': false,
					'cols': [
						{type:'id'},
						{name:'Name', text:LANG('广告位名称'), type:'index'},
						{name: 'WidthHeight', text:LANG('尺寸'), align:'center', render: 'renderSize'},
						{name: 'operation', type: 'op', width: 70, align:'center', render: 'renderOperation'}
					],
					'hasTab': false,
					'hasAmount': false,
					'hasExport': false,
					'opClick': true,
					'reqType': 'websocket',
					'pager':{
						'size': 10,
						'showSizeTypes': 0
					}
				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function() {
			var c = this.getConfig();
			this.createAsync('grid', '@base/grid.baseNoDate', c.gridConfig);
		},
		renderOperation: function(index, val, row) {
			return  '<a href="#" data-op="remove">' + LANG("删除") + '</a>';
		},
		renderSize: function(id, val, row){
			return row.Width + '*' + row.Height;
		},
		reset: function() {
			this.$value = null;
			this.$.grid.reset();
			return this;
		},
		setParam: function(param){
			this.$param = param;
			return this;
		},
		setValue: function(value) {
			this.$value = util.clone(value);
			this.$.grid.setParam(util.extend({
				ids: value.PositionIds.join(',')
			}, this.$param)).load();
			return this;
		},
		getValue: function() {
			return this.$value;
		},
		removeGroup: function(data) {
			var value = this.$value;
			var grid = this.$.grid;

			if (!value) {
				return;
			}

			pubjs.confirm(LANG('真的要删除广告位: %1 吗?', data.Name), function(isOk) {
				if (isOk) {
					util.remove(value.PositionIds, data._id);
					grid.setParam({
						ids: value.PositionIds.join(',')
					}).load();
				}
			});
		},
		onListOpClick: function(ev){
			var param = ev.param;

			if (ev.source === this.$.grid){
				switch(param.op) {
					case 'remove':
						this.removeGroup(param.data);
					break;
				}
				return false;
			}
		},
		onSizeChange: function(){
			this.getLayout().update();
			return false;
		},
		onDialogOk: function() {
			var data = this.getValue();
			this.fire('viewChange', data);
			this.hide();
			return false;
		}
	});

	exports.base = Base;
});
